import re
import xlwt
from openpyxl import Workbook
from numpy import *
import pymysql
import numpy as np
import matplotlib.pyplot as plt

# 创建一个excel
wb = Workbook()  # 创建一个工作表
ws = wb.active  # ws操作sheet页
ws1 = wb.create_sheet('Mysheetfirst', 0)

ws1['A1'] = '时间'
ws1['B1'] = 'usr%'
# 读取参数文件，将文件根据每行的空格隔开
fp1 = open("cpu_usage_all.log")
count = 0
str_list = []
for line in fp1.readlines():
    str_list = line.split()
    if count > 1 & count < 500:
        if str_list:
            if len(str_list) > 4:
                if str_list[3] != '%usr' and str_list[2] != 'all':
                    ws1.cell(row=count, column=1, value=str_list[0])
                    ws1.cell(row=count, column=2, value=str_list[3])
                    print("成功", count)
    count += 1

wb.save('D:\cpu.xlsx')  # 保存

wb.close()
fp1.close()
